clear all

%%transform the value of the labels from pixeis to coordinates
%more than one worm at the same time; data of different worms is in
%different colums
xlabpix = xlsread ('PA to OP 7.xlsx', 1);
ylabpix = xlsread ('PA to OP 7.xlsx', 2);
conver = xlsread ('PA to OP 7.xlsx', 3);
n = size (xlabpix, 2);
% convert the x coordinates
xlab = convertx (xlabpix,conver);
ylab = converty (ylabpix, conver);

%% find the parameters to align to the PA stimuli
%set the same value for the position of the PA stimuli - I decided these
%values in an arbitrary way to fit well in the graph
OPx = 17500;
OPy = 12500;

%found the distance between my stimulus and the specificed PA stimuli
xdistOP = OPx - xlab;
ydistOP = OPy - ylab;

%% call the values of speed, x and y
speedall = xlsread ('RRF3_exp30_N2_speed_all_F1');%all the individuals to make the scale
speed = xlsread ('PA to OP 7.xlsx',4);
hor = xlsread ('PA to OP 7.xlsx',5);
ver = xlsread ('PA to OP 7.xlsx',6);

%% normalize the x and y position to the OP stimulus

horfinal = [];%for x values horizontally
for i = 1:n;
    xdis (i) = xdistOP(1,i);
    hornorm = hor (:, i) + xdis (i);
    a = hornorm;
    horfinal = [horfinal a];
end

verfinal = [];% for y values, verticaly
for i = 1:n;
    ydis (i) = ydistOP(1,i);
    vernorm = ver (:, i) + ydis (i);
    b = vernorm;
    verfinal = [verfinal b];
end

%% find the max and min values of speed for all experiment
speedrange = velocidadef (speedall);
speedminall = speedrange (1,1);
speedmaxall = speedrange (1,2);
% normalize the speed
speedNorm = (speed - speedminall) / ( speedmaxall - speedminall) ;
%%
%% make the plot
figure
for i=1:n
    x= horfinal (:,i);
    y= verfinal (:,i); 
    speedNormi = speedNorm (:,i);
    % to draw a circle centered in the position of the stimulus
    center = [17500, 12500];
    radius = [2000];
%figure
%subplot (2,3, i);
viscircles (center, radius, 'Color', 'k');
hold on
% our plot with the coordinates x and y, size of dots 10 and color based on
% our heat map
scatter(x,y,10,speedNormi, 'filled')
colormap('jet')
box off
axis ([0,25000, 0, 25000]);
axis square
s1 = 'Worm';
s2 = i;
s3 = num2str(s2);
name = strcat(s1,s3);
title(name)
xlabel ('x')
ylabel ('y')
hold on;
end

%%  make the scale
speed0 = speedNorm (1,1); %find the value of 0 when normalized; 1st colum 1st row 
%because in the speed values the first values are always speed = 0
c = colorbar;
c.Label.String = 'Speed (um/s)';
caxis([0 1.0])% our speed is normalized from 0 to 1 so will match the scale
colorbar('Ticks',[0,speed0,1.0],...
         'TickLabels',{speedminall,0,speedmaxall});
